package com.hsy.order.setting.ui

import android.content.Intent
import android.graphics.Color
import android.text.InputType
import android.view.View

import com.flux.action.RxStoreChange
import com.flux.utils.GlideUtil
import com.hsy.order.R
import com.hsy.order.base.Config
import com.hsy.order.base.action.ActionType
import com.hsy.order.base.ui.activity.WebViewActivity
import com.hsy.order.base.ui.fragment.BaseFragment
import com.hsy.order.base.ui.widget.InputDialog
import com.hsy.order.common.ui.MainActivity
import com.hsy.order.login.ui.LoginActivity
import com.hsy.order.setting.action.SettingActionCreator
import com.hsy.order.setting.store.SettingStore
import com.hsy.order.utils.CommonUtils
import kotlinx.android.synthetic.main.fragment_setting.*

/**
 * 消息Fragment
 * Created on 2017/12/4.
 *
 * @author yang
 */
class SettingFragment : BaseFragment<SettingStore>(), View.OnClickListener {

    /**
     * 事件产生器
     */
    private val creator = SettingActionCreator(dispatcher, manager)
    /**
     * 数据缓存器
     */
    override var store = SettingStore(dispatcher)
    /**
     * 布局文件
     */
    override val fragmentLayout: Int
        get() = R.layout.fragment_setting

    override fun initView(view: View?) {
        shopState.setOnClickListener(this)
        settingPrint.setOnClickListener(this)
        logout.setOnClickListener(this)
        //因为使用onCheckedChange的话会导致"change->修改事件->获取修改->change"的循环，所以此处使用onClick
        settingAutoAccept.setOnClickListener(this)
        settingAutoDispatch.setOnClickListener(this)
        settingHow.setOnClickListener(this)
        settingPrintNum.setOnClickListener(this)
    }

    override fun initData() {
        super.initData()
        CommonUtils.showProgressDialog(context)
        creator.getUerInfo()
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.shopState -> {
                CommonUtils.showProgressDialog(context)
                creator.setShop("is_open", if (store.userInfo.is_open == "1") "2" else "1")
            }
            R.id.settingPrint -> (activity as MainActivity).setBluetooth()
            R.id.settingAutoAccept -> {
                CommonUtils.showProgressDialog(context)
                creator.setShop("auto_accept_order", if (settingAutoAccept.isChecked) "1" else "2")
            }
            R.id.settingAutoDispatch -> {
                CommonUtils.showProgressDialog(context)
                creator.setShop("auto_pai", if (settingAutoDispatch.isChecked) "1" else "2")
            }
            R.id.logout -> {
                CommonUtils.clearToken()
                startActivityWithTaskCleared(LoginActivity::class.java)
            }
            R.id.settingHow -> {
                val intent = Intent(context, WebViewActivity::class.java)
                intent.putExtra("url", Config.WEB_HOW)
                intent.putExtra("type", "")
                intent.putExtra("title", "")
                intent.putExtra("backButtonVisibility", View.VISIBLE)
                startActivity(intent)
            }
            R.id.settingPrintNum -> {
                val dialog = InputDialog(context!!) {
                    CommonUtils.savePrintNum(it.toInt())
                    printNum.text = it
                }
                dialog.setInputType(InputType.TYPE_CLASS_NUMBER)
                dialog.setHint("请输入每次打印的份数")
                dialog.show()
            }
        }
    }

    /**
     * 界面响应统一处理
     * @param change 需要界面响应的变化
     */
    override fun onRxStoreChanged(change: RxStoreChange) {
        if (change.storeId == store.tag) when (change.type) {
            ActionType.GET_USER_INFO -> {
                CommonUtils.hideProgressDialog()
                GlideUtil.load(context!!, store.userInfo.img, shopImage)
                shopName.text = store.userInfo.name
                if (store.userInfo.is_open == "1") {
                    shopState.text = "营业中"
                    shopState.setBackgroundColor(0xff009D37.toInt())
                } else {
                    shopState.text = "休息中"
                    shopState.setBackgroundColor(Color.BLACK)
                }
                CommonUtils.saveAutoAccept(if (store.userInfo.is_auto_accept == "1") CommonUtils.getUserId() else "a")
                CommonUtils.saveAutoDispatch(if (store.userInfo.is_auto_accept == "1") CommonUtils.getUserId() else "a")
                CommonUtils.saveReloadInterval(store.userInfo.reload_time.toLong())
                settingAutoAccept.isChecked = store.userInfo.is_auto_accept == "1"
                printNum.text = CommonUtils.getPrintNum().toString()
            }
            ActionType.SET_SHOP -> creator.getUerInfo()
        }
    }
}